home *** CD-ROM | disk | FTP | other *** search
- /*****************************************************************************************
-
- NTBDemo.c - demo/test program for NeoTextBox
-
- Written by Bryan K. Ressler (Beaker), 8/30/91
-
- updated for the wonderful universe of PowerPC by Pete Gontier (Gurgle), 3/31/97
-
- *****************************************************************************************/
-
- #define BEAKER
-
- /** INCLUDES ****************************************************************************/
-
- #include "NTBDemo.h" /* NTBDemo stuff */
- #include "Utilities.h" /* Utility stuff */
- #include "NeoTextBox.h" /* NeoTextBox module */
-
- #ifndef __RESOURCES__
- # include <Resources.h>
- #endif
-
- #ifndef __FONTS__
- # include <Fonts.h>
- #endif
-
- #ifndef __TEXTUTILS__
- # include <TextUtils.h>
- #endif
-
- #ifndef __TOOLUTILS__
- # include <ToolUtils.h>
- #endif
-
- /** GLOBALS *****************************************************************************/
-
- short gJustRadio; /* Currently-set justification radio button */
- short gFontRadio; /* Currently-set font radio button */
-
- /** STATICS ****************************************************************************/
-
- static SysEnvRec gEnvironment; /* Our operating environment */
- static UInt8 gsBlankStr[1]; /* Blank string */
- static short gsTextRadio; /* Currently-set text radio button */
- static Boolean gsVariable; /* State of "Variable line height" checkbox */
- static short gsNumLines; /* The number of line in the wrapped text total */
- static short gsEndY; /* The endingY from NeoTextBox item */
- static short gsLHUsed; /* The line height used based on the given text spec */
- static Str255 gsInfoStr; /* The string displayed in the info item */
-
- /****************************************************************************************/
- static pascal void UpdateParms(DialogPtr dialog)
- {
- UInt8 convBuffer1[40],convBuffer2[40],convBuffer3[40];
-
- NumToString(gsEndY,convBuffer1);
- NumToString(gsLHUsed,convBuffer2);
- NumToString(gsNumLines,convBuffer3);
- ParamText(convBuffer1,convBuffer2,convBuffer3,gsBlankStr);
-
- InvalItem(dialog,kValuesItem,0,0);
- }
-
- /****************************************************************************************/
- static pascal void NTBItem(WindowPtr theWindow,short itemNum)
- {
- short aType;
- Rect theBox;
- Handle aHandle,text;
- long textLen;
- short just,lineHeight;
- Boolean oldPreserve,oldPreferred;
- short oldLH,oldNL,oldEY;
-
- GetDialogItem(theWindow,itemNum,&aType,&aHandle,&theBox);
- PenNormal(); PenPat(&qd.gray);
- ForeColor(blackColor);
- FrameRect(&theBox);
- PenNormal();
-
- InsetRect(&theBox,4,4);
- EraseRect(&theBox);
-
- text = GetResource('TEXT',(gsTextRadio == kShannonText) ? kShannonID : kBarkerID);
- if (text) {
- oldLH = gsLHUsed; /* Remember these so we can check for diffs */
- oldNL = gsNumLines;
- oldEY = gsEndY;
-
- just = RadioToJust(); /* Find out what justification to use */
- textLen = GetHandleSize(text); /* Determine text length */
- HLock(text); /* Lock text - NeoTextBox may move memory */
-
- TextParms(kSave); /* Save current port text parameters */
-
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
-
- oldPreferred = GetOutlinePreferred(); /* Save old, prefer TrueType */
- SetOutlinePreferred(true);
-
- if (gsVariable) {
- oldPreserve = GetPreserveGlyph(); /* Save old, preserve glyphs */
- SetPreserveGlyph(true);
- }
-
- lineHeight = (gsVariable) ? -1 : 0;
-
- /* This is the fancy-schmancy way, see InfoItem for a simple example */
- gsNumLines = NeoTextBox(*text,textLen,&theBox,just,lineHeight,&gsEndY,&gsLHUsed);
-
- SetOutlinePreferred(oldPreferred);
- if (gsVariable)
- SetPreserveGlyph(oldPreserve);
-
- TextParms(kRestore); /* Restore the port's old text parameters */
-
- if (gsEndY <= theBox.bottom) {
- PenPat(&qd.ltGray); /* Draw a light gray line below the text */
- PenMode(patOr);
- ForeColor(blueColor);
- MoveTo(theBox.left,gsEndY);
- LineTo(theBox.right - 1,gsEndY);
- ForeColor(blackColor);
- PenNormal();
- }
-
- if (gsLHUsed != oldLH || gsNumLines != oldNL || gsEndY != oldEY)
- UpdateParms(theWindow); /* Redraw the stats if they've changed */
- }
- }
-
- /****************************************************************************************/
- static pascal void NTBInfoItem(WindowPtr theWindow,short itemNum)
- {
- short aType;
- Rect theBox;
- Handle aHandle;
-
- GetDialogItem(theWindow,itemNum,&aType,&aHandle,&theBox);
- ForeColor(blackColor);
-
- EraseRect(&theBox);
-
- TextParms(kSave); /* Save current port text parameters */
-
- TextFont(GetAppFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(9);
- TextMode(srcOr);
-
- /* This is the simple, TextBox-like way to call NeoTextBox */
- NeoTextBox(gsInfoStr + 1,*gsInfoStr,&theBox,GetSysDirection(),0,nil,nil);
-
- TextParms(kRestore); /* Restore the port's old text parameters */
- }
-
- /****************************************************************************************/
- static pascal void RedrawInfo(DialogPtr dialog)
- {
- InvalItem(dialog,kInfoItem,0,0);
- BeginUpdate(dialog);
- UpdateDialog(dialog,dialog->visRgn);
- EndUpdate(dialog);
- }
-
- /****************************************************************************************/
- static pascal void NTBPerformance(DialogPtr dialog)
- {
- UInt8 buf1[40],buf2[40],buf3[40],buf4[40];
- Handle testTextHdl;
- char *testText;
- long testLen;
- Rect box;
- short i,just,percentFaster;
- long startTime,endTime,textBoxTime,neoTextBoxTime;
- Fixed factor;
- Boolean goForIt = true;
-
- just = RadioToJust(); /* Set up justification */
- testTextHdl = GetResource('TEXT',kTestTextID);
-
- if (just == ntbJustFull) {
- goForIt = false;
- StopAlert(kNoCanDoAlrtID,nil);
- }
-
- if (goForIt) {
- NumToString(kTestIterations,buf1); /* Put stats dialog up */
- ParamText(buf1,gsBlankStr,gsBlankStr,gsBlankStr);
- goForIt = (NoteAlert(kDoPerfAlrtID,nil) == kOkayButton);
- }
-
- if (goForIt && testTextHdl) {
- testLen = GetHandleSize(testTextHdl);
- HLock(testTextHdl);
- testText = *testTextHdl;
-
- GetItemRect(dialog,kTextArea,&box);
- InsetRect(&box,4,4);
-
- GetIndString(gsInfoStr,kStringsID,kCachingPass);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- TETextBox(testText,testLen,&box,just);
- EraseRect(&box);
- NeoTextBox(testText,testLen,&box,just,0,nil,nil);
- TextParms(kRestore); /* Restore port's text settings */
-
- GetIndString(gsInfoStr,kStringsID,kTestingTextBox);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- startTime = TickCount();
- for (i = 0; i < kTestIterations; i++)
- TETextBox(testText,testLen,&box,just);
- endTime = TickCount();
- textBoxTime = endTime - startTime;
- TextParms(kRestore); /* Restore port's text settings */
-
- GetIndString(gsInfoStr,kStringsID,kTestingNeoTextBox);
- RedrawInfo(dialog);
- TextParms(kSave); /* Save text parameters */
- TextFont(RadioToFont()); /* Set up the port's text settings */
- TextFace(0);
- TextSize(GetDefFontSize());
- TextMode(srcOr);
- startTime = TickCount();
- for (i = 0; i < kTestIterations; i++) {
- EraseRect(&box);
- NeoTextBox(testText,testLen,&box,just,0,nil,nil);
- }
- endTime = TickCount();
- neoTextBoxTime = endTime - startTime;
- TextParms(kRestore); /* Restore port's text settings */
-
- HUnlock(testTextHdl);
-
- NumToString(kTestIterations,buf1); /* Put stats dialog up */
- NumToString(textBoxTime,buf2);
- NumToString(neoTextBoxTime,buf3);
- factor = FixRatio(textBoxTime,neoTextBoxTime) - Long2Fix(1);
- percentFaster = FixRound(FixMul(factor,Long2Fix(100)));
- NumToString(percentFaster,buf4);
- ParamText(buf1,buf2,buf3,buf4);
- NoteAlert(kPerfAlrtID,nil);
-
- GetIndString(gsInfoStr,kStringsID,kStdInfo); /* Restore info item text */
- InvalItem(dialog,kInfoItem,0,0);
- InvalItem(dialog,kTextArea,4,4);
- }
-
- UpdateParms(dialog);
- }
-
- /****************************************************************************************/
- static pascal void SetupDialog(DialogPtr testDialog)
- {
- short i;
-
- gJustRadio = JustToRadio(GetSysDirection()); /* Default to system justification */
- gsVariable = false; /* Default to default line height */
- gFontRadio = kAppFont; /* Default to application font */
- gsTextRadio = kShannonText; /* Default to Shannon text */
-
- GetIndString(gsInfoStr,kStringsID,kStdInfo); /* Set up info item */
-
- SetValue(testDialog,gJustRadio,1); /* Set initial values */
- SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
- SetValue(testDialog,gFontRadio,1);
- SetValue(testDialog,gsTextRadio,1);
-
- for (i = kBoxOne; i <= kLastBox; i++)
- UserItem(testDialog,i,BoxItem); /* Install box user items */
- for (i = kDividerOne; i <= kLastDivider; i++)
- UserItem(testDialog,i,GrayBoxItem); /* Install divider user items */
- UserItem(testDialog,kTextArea,NTBItem); /* Install the main text item */
- UserItem(testDialog,kInfoItem,NTBInfoItem); /* Install the information item */
-
- NTBItem(testDialog,kTextArea); /* Draw (to set Y/line height vars) */
-
- ShowWindow(testDialog); /* Put it on the screen */
- }
-
- /****************************************************************************************/
- static pascal void PoseDialog(DialogPtr testDialog)
- {
- short itemHit;
-
- do {
- ModalDialog(nil,&itemHit);
- switch (itemHit) {
- case kPerfButton:
- NTBPerformance(testDialog);
- break;
- case kJustLeft: /* Justification cluster */
- case kJustCenter:
- case kJustRight:
- case kJustFull:
- if (gJustRadio != itemHit) {
- SetValue(testDialog,gJustRadio,0);
- gJustRadio = itemHit;
- SetValue(testDialog,gJustRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- case kVariableHeight: /* Variable height checkbox */
- gsVariable = !gsVariable;
- SetValue(testDialog,kVariableHeight,gsVariable ? 1 : 0);
- InvalItem(testDialog,kTextArea,4,4);
- break;
- case kAppFont: /* Font cluster */
- case kTimes:
- case kHelvetica:
- if (gFontRadio != itemHit) {
- SetValue(testDialog,gFontRadio,0);
- gFontRadio = itemHit;
- SetValue(testDialog,gFontRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- case kShannonText: /* Test cluster */
- case kLiterature:
- if (gsTextRadio != itemHit) {
- SetValue(testDialog,gsTextRadio,0);
- gsTextRadio = itemHit;
- SetValue(testDialog,gsTextRadio,1);
- InvalItem(testDialog,kTextArea,4,4);
- }
- break;
- default:
- break;
- }
- } while (itemHit != kQuitButton);
- }
-
- /****************************************************************************************/
- static pascal void DemoNTB(void)
- {
- GrafPtr savePort;
- DialogPtr testDialog;
-
- GetPort(&savePort);
- testDialog = GetNewDialog(kTestDlgID,nil,(WindowPtr)-1);
- if (testDialog) {
- SetPort(testDialog);
- SetupDialog(testDialog);
- PoseDialog(testDialog);
- DisposeDialog(testDialog);
- }
- SetPort(savePort);
- }
-
- /****************************************************************************************/
- static pascal Boolean Initialize(void)
- {
- OSErr err;
-
- InitGraf(&qd.thePort); /* The usual Mac inits */
- InitFonts();
- InitWindows();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- *gsBlankStr = 0;
- err = SysEnvirons(2,&gEnvironment);
- return((err || gEnvironment.systemVersion < 0x0600) ? false : true);
- }
-
- /****************************************************************************************/
- void main(void)
- {
- if (Initialize())
- DemoNTB();
- }
-